package com.dataimport.service.impl;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.sourceentity.StudentScoreInfo;
import com.dataimport.entity.targetentity.SchoolCalendar;
import com.dataimport.entity.targetentity.Score;
import com.dataimport.mapper.SchoolCalendarMapper;
import com.dataimport.mapper.ScoreMapper;
import com.dataimport.mapper.SelectSchoolCalendarMapper;
import com.dataimport.mapper.StudentScoreInfoMapper;
import com.dataimport.service.ScoreService;
import com.dataimport.utils.HasNullFieldUtil;
import com.dataimport.utils.SchoolCalendarCache;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分数服务impl
 *
 * @author 章珑赢
 * @date 2022/09/29
 */
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper,Score> implements ScoreService {
    @Autowired
    private StudentScoreInfoMapper studentScoreInfoMapper;
    @Autowired
    private SchoolCalendarMapper schoolCalendarMapper;
    @Override
    public void dealWithScore() {
        //查询mysql2数据库，导出全部学生成绩信息
        DynamicDataSourceContextHolder.push("mysql2");
        List<StudentScoreInfo> studentScoreInfos = studentScoreInfoMapper.selectList(null);
        DynamicDataSourceContextHolder.poll();
//        数据清洗
        if (CollectionUtils.isNotEmpty(studentScoreInfos)) {
            // 进行拆解封装
            List<Score> scoreList= studentScoreInfos.stream().parallel().map(studentScoreInfo -> {
                Score score = new Score();
                //校历拼接查询
                int  year = Integer.parseInt(studentScoreInfo.getXn())+1;
                String years = studentScoreInfo.getXn()+"-"+year+"-"+studentScoreInfo.getXq();
                int calendar =  SchoolCalendarCache.selectSchoolCalendar(schoolCalendarMapper).get(years);
                score.setCalendarId(Long.valueOf(calendar));
                score.setStudentCode(Long.parseLong(studentScoreInfo.getXh()));
                score.setCourseCode(studentScoreInfo.getKcbm());
                score.setObtainingCredits(Double.valueOf(studentScoreInfo.getXf()));
                score.setGradePoint(Double.parseDouble(studentScoreInfo.getXfjd()));
                score.setAchievement(studentScoreInfo.getKscj());
                return score ;
            }).collect(Collectors.toList());
            scoreList.forEach(System.out::println);

            insertScore(scoreList);
        }
    }
    @Transactional(rollbackFor = Exception.class)
    void insertScore(List<Score> scoreList){
        DynamicDataSourceContextHolder.push("mysql1");
        this.saveBatch(scoreList);
        DynamicDataSourceContextHolder.poll();
    }
}
